<template>
    <div class="top-search">
        <van-search v-model.trim="value" placeholder="搜索商品名称" shape="round" @click="goSearch" />
    </div>
    <div class="content">
        <div class="sortNav">
            <div v-for="(a, index) in filter" :key="a.category_id" class="nav" :class="{ active: index == currentIndex }"
                @click="changeIndex(index, a.category_id)">
                <div :class="{ left: index == currentIndex }"></div>{{ a.category_name }}
            </div>

        </div>

        <div class="sortList">
            <CompoentList :id="id" class="hhh" :height="height" />
        </div>

    </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { sortList } from '@/api/sort.js'
import CompoentList from '../../component/CompoentList.vue'
import { useRouter } from 'vue-router';
const height = ref('')
const value = ref('');
const renderData = ref('')
const currentIndex = ref(1)
const id = ref(874)
const filter = ref([])
const sortId = ref([])
const sortList1 = sortList().then(function (response) {

    renderData.value = response.data.data
    filter.value = renderData.value.filter((item) => item.category_list)
    filter.value = filter.value.splice(2)
    for (let i = 0; i < filter.value.length; i++) {
        sortId.value.push(filter.value[i].category_id)
    }
})
const changeIndex = (index, changeID) => {
    currentIndex.value = index
    id.value = changeID

}

onMounted(function () {
    const obj = document.querySelector<HTMLElement>(".sortList")!
    obj.addEventListener("scroll", handleScroll, true)
    sortList1
})
const handleScroll = () => {
    const obj = document.querySelector<HTMLElement>(".sortList")!
    height.value = obj.scrollTop
}
const router = useRouter()
const goSearch = () => {
    router.push({
        name:'search'
    }
    )
}

</script>

<style scoped>
.v-enter-active,
.v-leave-active {
    transition: opacity 2s;
}

.v-enter-from,
.v-leave-to {
    opacity: 0;
}

.top-search {
    width: 100%;
    height: 50px;
    background-color: antiquewhite;
    /* position: fixed;
    top: 0; */

}

.van-search {

    padding: 10px 10px 10px 30px;
}

.content {
    display: flex;
}

.sortNav {
    width: 20%;

}

.sortList {
    margin-left: 20px;
    margin-top: 10px;
    width: 80%;
    height: 790px;

    overflow-y: scroll;
}

.nav {
    width: 87.48px;
    height: 51.8px;
    text-align: center;
    line-height: 51.8px;
    font-size: 12px;
    color: #666;
    position: relative;
}

.left {
    width: 3px;
    height: 15px;
    background-color: orangered;
    position: absolute;
    top: 18px;
    left: 0;
}

.active {
    color: black;
    font-size: 12px;
}

.distance {
    margin-top: 12px;
}</style>
